package org.gpp.proj1.ui.gui;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.WindowConstants;

import org.gpp.proj1.ui.gui.command.SRGGUICommandFactory;

public class MainWindow extends JFrame {

	private static final long serialVersionUID = 2507299731915051979L;
	private static final String LOGO_FILE = "res/logo.jpg";
	private final SRGGUIView view;

	public MainWindow(SRGGUIView view) {
		this.view = view;

		this.initComponents();

		this.setLocationRelativeTo( null );

		this.setVisible( true );
	}

	private void initComponents() {
		this.setTitle("SomeRobotGame V0.1a");

		JLabel greetingsLabel = new JLabel();
		JLabel editRobotsLabel = new JLabel();
		JLabel statsLabel = new JLabel();
		JLabel optionsLabel = new JLabel();		
		JLabel startGameLabel = new JLabel();		
		JLabel buyPartsLabel = new JLabel();
		JLabel sellPartsLabel = new JLabel();
		JLabel inventoryLabel = new JLabel();
		JLabel logoLabel = new JLabel();

		JButton buyPartsButton = new JButton();
		JButton sellPartsButton = new JButton();
		JButton partInventoryButton = new JButton();
		JButton startGameButton = new JButton();
		JButton quitButton = new JButton();
		JButton robotEditorButton = new JButton();		
		JButton optionsButton = new JButton();
		JButton statsButton = new JButton();

		JSeparator separator = new JSeparator();		


		greetingsLabel.setFont(new java.awt.Font("Dialog", 1, 18));
		greetingsLabel.setText("Hello, " + view.getController().getCurrentProfile().getName() );

		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		startGameButton.setText("Start Game");
		startGameButton.setActionCommand( SRGGUICommandFactory.START_GAME_COMMAND );
		startGameButton.addActionListener( view );

		startGameLabel.setText("Start a new game");

		buyPartsButton.setText("Buy parts");
		buyPartsButton.setActionCommand( SRGGUICommandFactory.OPEN_PART_BUYER_WINDOW_COMMAND );
		buyPartsButton.addActionListener( view );

		buyPartsLabel.setText("Buy robot parts");
		
		sellPartsButton.setText("Sell parts");
		sellPartsButton.setActionCommand( SRGGUICommandFactory.OPEN_PART_SELLER_WINDOW_COMMAND );
		sellPartsButton.addActionListener( view );
		
		sellPartsLabel.setText("Sell robot parts");
		
		partInventoryButton.setText("Part inventory");
		partInventoryButton.setActionCommand( SRGGUICommandFactory.OPEN_PART_INVENTORY_WINDOW_COMMAND );
		partInventoryButton.addActionListener( view );
		
		inventoryLabel.setText( "View your robot parts" );

		quitButton.setText("Quit");		

		editRobotsLabel.setText("Edit or create your robots");

		robotEditorButton.setText("Robot Editor");
		robotEditorButton.setActionCommand( SRGGUICommandFactory.ROBOT_EDITOR_COMMAND );
		robotEditorButton.addActionListener( view );

		optionsLabel.setText("Configure the game");

		optionsButton.setText("Options");
		optionsButton.setEnabled( false );

		statsLabel.setText("View your battle statistics");

		statsButton.setText("Stats");
		statsButton.setEnabled( false );

		logoLabel.setIcon(new ImageIcon( LOGO_FILE ));

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout.createSequentialGroup()
						.addContainerGap()
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
								.add(logoLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.add(separator, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
								.add(org.jdesktop.layout.GroupLayout.TRAILING, quitButton)
								.add(greetingsLabel)
								.add(layout.createSequentialGroup()
										.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
												.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
														.add(buyPartsButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
														.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
														.add(buyPartsLabel)
														.add(66, 66, 66))
														.add(layout.createSequentialGroup()
																.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
																		.add(org.jdesktop.layout.GroupLayout.LEADING, optionsButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																		.add(org.jdesktop.layout.GroupLayout.LEADING, statsButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																		.add(org.jdesktop.layout.GroupLayout.LEADING, robotEditorButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																		.add(org.jdesktop.layout.GroupLayout.LEADING, sellPartsButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 144, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
																				.add(sellPartsLabel)
																				.add(editRobotsLabel)
																				.add(statsLabel)
																				.add(optionsLabel))))
																				.add(1, 1, 1))
																				.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
																						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
																								.add(org.jdesktop.layout.GroupLayout.LEADING, partInventoryButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
																								.add(startGameButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
																								.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
																								.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
																										.add(inventoryLabel)
																										.add(startGameLabel))
																										.add(56, 56, 56)))
																										.addContainerGap())
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout.createSequentialGroup()
						.addContainerGap()
						.add(logoLabel)
						.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
						.add(greetingsLabel)
						.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
								.add(startGameButton)
								.add(startGameLabel))
								.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
								.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
										.add(partInventoryButton)
										.add(inventoryLabel))
										.add(4, 4, 4)
										.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
												.add(buyPartsButton)
												.add(buyPartsLabel))
												.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
												.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
														.add(sellPartsButton)
														.add(sellPartsLabel))
														.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
														.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
																.add(robotEditorButton)
																.add(editRobotsLabel))
																.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
																.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
																		.add(statsButton)
																		.add(statsLabel))
																		.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
																				.add(optionsButton)
																				.add(optionsLabel))
																				.add(15, 15, 15)
																				.add(separator, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																				.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
																				.add(quitButton)
																				.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		pack();
	}
}
